package Server;

import Util.Constant;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.execution.ExecutionHandler;
import org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

/**
 * @Author:AwakeningCode
 * @Date: Created in 23:01 2018\1\18 0018
 */
public class NettyServer {

    public static String host = "127.0.0.1";

    static ExecutionHandler executionHandler = new ExecutionHandler(new OrderedMemoryAwareThreadPoolExecutor(16,1048576,1048576));

    public static void main(String[] args) {
        //ChannelFactory
        final ChannelFactory channelFactory = new NioServerSocketChannelFactory(
                //Boss线程池，处理socket请求
                Executors.newCachedThreadPool(),
                //Worker线程池，由于使用的是NIO，1个Worker线程可以管理多个Channel
                Executors.newCachedThreadPool()
        );
        //ServerBootstrap
        ServerBootstrap bootstrap = new ServerBootstrap(channelFactory);

        ServerPipeLineFactory serverPipeLineFactory = new ServerPipeLineFactory(executionHandler);
        bootstrap.setPipelineFactory(serverPipeLineFactory);

        //禁用nagle算法
        bootstrap.setOption("child.tcpNoDelay",true);
        //启用TCP保活检测
        bootstrap.setOption("child.keepAlive",true);

        //监听5个端口
        bootstrap.bind(new InetSocketAddress(Constant.p1));
        System.out.println("Listening port " + Constant.p1 + "...");
        bootstrap.bind(new InetSocketAddress(Constant.p2));
        System.out.println("Listening port " + Constant.p2 + "...");
        bootstrap.bind(new InetSocketAddress(Constant.p3));
        System.out.println("Listening port " + Constant.p3 + "...");
        bootstrap.bind(new InetSocketAddress(Constant.p4));
        System.out.println("Listening port " + Constant.p4 + "...");
        bootstrap.bind(new InetSocketAddress(Constant.p5));
        System.out.println("Listening port " + Constant.p5 + "...");
    }
}
